#include "compiler.h"
#include <stdio.h>
#include <stdint.h>

#define LINE_LENGTH 80          /* Change if you need */

#define In_DELETE 0x7F          /* ASCII <DEL> */
#define In_EOL '\r'             /* ASCII <CR> */
#define In_SKIP '\3'            /* ASCII control-C */
#define In_EOF '\x1A'           /* ASCII control-Z */

#define Out_DELETE "\x8 \x8"    /* VT100 backspace and clear */
#define Out_SKIP "^C\n"         /* ^C and new line */
#define Out_EOF "^Z"            /* ^Z and return EOF */

/****************************************************************************
**
** Putchar function needed for ICC
**
** Parameters: send_char, character to send
**
** Returns: Sent character
**
****************************************************************************/

u8 putuartchar(unsigned char send_char)
{
	while (!(UCSRA & 0x20));// wait xmit ready
	UDR = (unsigned char) send_char;
	return(send_char);
}

u8 _low_level_get(void)
{
/* Wait for data to be received */
while ( !(UCSRA & (1<<RXC)) );
/* Get and return received data from buffer */
  return UDR;
}

char get_char(void)
{
/* Wait for data to be received */
if ( (UCSRA & (1<<RXC)) )
/* Get and return received data from buffer */
  return UDR;
  return 0;
}

/******* ************************
 put message from SRAM to UART
*******************************/
void put_message(char *s)
{
  while (*s)
    putuartchar(*s++);
}

void put_message2(char *s, int length)
{
	int i;
	for (i = 0; i < length; i++)
		putuartchar(s[i]);
}


void putint(int value )
{
    uint8_t puffer[20];
 
    sprintf( puffer, "%i\r\n", value );
    put_message( puffer );
}

void print(char* s, int value)
{
	uint8_t puffer[20];
 
    sprintf( puffer, s, value );
    put_message( puffer );
}


/****************************************************************************
**
** Initializes UART0
**
** Parameters: None
**
** Returns: None
**
****************************************************************************/
void uart_init(void)
{
/* UART0 initialisation */

UCSRB = 0x00; /* disable while setting baud rate */
UCSRA = 0x00;
UCSRC = 0x06; // 8 bit data
UBRRL = 12; // set baud rate lo (38400 @ 8 MHz)
UBRRH = 0x00; /* set baud rate hi */
UCSRB = 0x18; //RX and TX are enabled
}



